import { S, U, getRequestId, mapField } from '../../lib/backend'
import Q from 'q'
import axios from 'axios'
import env from '../../config/common'
import N from '../../lib/notification'
import { customFields, customSubmit } from '../mappings/customs'
import { UPDATE_DATA_DICTCTIONARY, UPDATE_SWITCH_SITE_STATUS } from '../mutation-types'

export default {
  // 获取客户列表
  getCustoms ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/customs' + frag
      ).then((response) => {
        if (response.data.results) {
          const customs = mapField(response.data.results.data, customFields, info)
          response.data.results.data = customs
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  getCustom ({ dispatch, commit, state, rootState }, info) {
    const params = '?' + U()
    let frag = ''
    if (typeof info['id'] !== 'undefined') {
      frag = '/' + info['id']
    }
    console.log(frag)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/customs' + frag + params
      ).then((response) => {
        if (response.data.results) {
          const customs = mapField(response.data.results, customFields, info)
          response.data.results = customs
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  saveCustom ({ dispatch, commit, state, rootState }, info) {
    let frag = '?' + U()
    if (typeof info['id'] !== 'undefined') {
      frag = '/' + info['id']
    }
    console.log(JSON.stringify(info))
    info = mapField(info, customSubmit)
    const params = S(info)
    return Q.Promise((success, error) => {
      axios.post(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/customs' + frag,
        params
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  getExports ({ dispatch, commit, state, rootState }, info) {
    return Q.Promise((success, error) => {
      axios.get(
        env.EXPORT_API_HOST + '/sites/' + rootState.auth.site_id + '/customs/export'
      ).then((response) => {
        if (response.data.results) {
          const data = response.data.results
          response.data.results = data
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  // 导出客户名单任务接口
  saveExport ({ dispatch, commit, state, rootState }, info) {
    console.log(JSON.stringify(info))
    const params = S({
      info: info,
      trace: {
        requestId: getRequestId()
      }
    })
    return Q.Promise((success, error) => {
      axios.post(
        env.EXPORT_API_HOST + '/sites/' + rootState.auth.site_id + '/customs/export',
        params,
        { headers: {
          'Content-Type': 'application/json;charset=UTF-8'
        }}
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  // 获取数据字典
  getDictionary ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      if (Object.keys(rootState.custom['data_dictionary']).length > 0) {
        success(rootState.custom['data_dictionary'])
      }
      axios.get(
        env.CRM_API_HOST + '/data_dictionary' + frag
      ).then((response) => {
        if (response.data.results) {
          const data = response.data.results
          response.data.results = data
          commit(UPDATE_DATA_DICTCTIONARY, data)
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  // 获取数据字典
  getSites ({ dispatch, commit, state, rootState }, info) {
    const frag = '?' + U(info)
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/index' + frag
      ).then((response) => {
        if (response.data.results) {
          const data = response.data.results
          response.data.results = data
        }
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },

  switchSite ({ dispatch, commit, state, rootState }, info) {
    let frag = '?' + U()
    const params = S(info)
    if (typeof info['site_id'] !== 'undefined') {
      frag = '/' + info['site_id']
    }
    return Q.Promise((success, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + rootState.auth.user.token + '/switch_site' + frag,
        params
      ).then((response) => {
        success(response)
      }).catch((err) => {
        error(err)
        console.log(err)
      })
    })
  },
  showSwitchSite ({ commit }, info) {
    commit(UPDATE_SWITCH_SITE_STATUS, true)
  },

  hideSwitchSite ({ commit }, info) {
    commit(UPDATE_SWITCH_SITE_STATUS, false)
  }
}
